package com.cqut.ktpspring.controller;

import com.cqut.ktpspring.pojo.Message;
import com.cqut.ktpspring.pojo.Result;
import com.cqut.ktpspring.service.MessageService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@CrossOrigin
@Slf4j
@RestController
public class MessageController {
    @Autowired
    private MessageService messageService;

    @GetMapping("/getNotReadCount")
    public Result getNotReadCount(@RequestParam String receiveId) {
        log.info("获得未读的数量");
        int count = messageService.getNotReadCount(receiveId);
        return Result.success("未读的数量是"+count);
    }

    @GetMapping("/getMessages")
    public Result getMessages(@RequestParam String receiveId, @RequestParam(value = "status", required = false) Integer status) {
        log.info("查询收到的消息{}，{}", receiveId, status);
        List<Message> messageList = messageService.getMessages(receiveId, status);
        return Result.success(messageList);
    }

    @PostMapping("/send")
    public Result send(@RequestBody Message message) {
        log.info("发送消息{}",message);
        messageService.send(message);
        return Result.success(message);
    }

    @PutMapping("/read")
    public Result read(@RequestParam List<String> ids) {
        log.info("标注已读");
        messageService.read(ids);
        return Result.success();
    }
}
